<template>
  <div class="login-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" title="登录">
      <van-icon name="cross" slot="left" @click="$router.back()" />
    </van-nav-bar>
    <!-- 登录表单 -->
    <van-form @submit="onSubmit" ref="loginFrom">
      <!-- 手机验证 -->
      <van-field v-model="user.mobile" name="mobile" placeholder="请输入手机号" :rules="userrulesForm.mobile" type="number" maxlength="11">
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
      <!-- 验证码验证 -->
      <van-field v-model="user.code" name="code" placeholder="请输入验证码" :rules="userrulesForm.code" type="number" maxlength="6">
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
        <!-- 获取验证码按钮 -->
        <template #button>
          <!-- 倒计时 -->
          <van-count-down :time="1000 * 3" format="ss s" v-if="countDown" @finish="countDown = false" />
          <van-button class="send-sms-btn" round size="small" type="default" native-type="button" @click="onSengSms" v-else>获取验证码</van-button>
        </template>
      </van-field>
      <div class="login-btn-warp">
        <van-button class="login-btn" block type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { LoginAPI, SendAPI } from '@/api/LoginAPI.js'
export default {
  name: 'LoginIndex',
  data() {
    return {
      user: {
        mobile: '13911111122',
        code: '246810'
      },
      userrulesForm: {
        mobile: [
          { required: true, message: '请输入手机号' },
          { pattern: /^(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/, message: '请输入正确手机号' }
        ],
        code: [
          { required: true, message: '请输入验证码' },
          { pattern: /\d{6}/, message: '请输入正确验证码' }
        ]
      },
      // 显示倒计时是否显示
      countDown: false
    }
  },
  methods: {
    async onSubmit() {
      // 获取表单数据
      const user = this.user
      // 在组件中必须通过this.$toast来调用Toast组件
      this.$toast.loading({
        message: '登录中...',
        forbidClick: true //禁用背景点击
        // duration: 2000 //持续时间，默认是2000
      })

      // 提交表单验证登录
      try {
        const { data } = await LoginAPI(this.user)
        console.log(data)
        this.$toast.success('登录成功')
        this.$store.commit('setUser', data.data)
        // 登录成功之后，跳转会登录页面
        this.$router.back()
      } catch (err) {
        if (err.response.status === 400) {
          this.$toast.fail('手机或验证码错误', err)
        } else {
          //外部错误抛出
          this.$toast.fail('登录失败,请稍后重试', err)
        }
      }
    },
    async onSengSms() {
      // 效验手机号
      try {
        await this.$refs.loginFrom.validate('mobile')
      } catch (err) {
        return console.log('验证失败', err)
      }
      // 通过验证显示倒计时
      this.countDown = true

      // 请求发送短信验证码
      try {
        await SendAPI(this.user.mobile)
        this.$toast.success('发送成功')
      } catch (err) {
        if (err.response.status === 429) {
          this.$toast.fail('请勿频繁操作，请稍后重试')
        } else {
          this.$toast.fail('发送失败，请稍后重试')
        }
      }
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  .toutiao {
    font-size: 37px;
  }
  .send-sms-btn {
    width: 189px;
    height: 46px;
    font-size: 22px;
    line-height: 46px;
    background-color: #ededed;
    color: #666666;
  }
  .login-btn-warp {
    margin: 53px 33px;
  }
  .login-btn {
    background-color: #6db4fb;
    border: none;
    font-size: 30px;
  }
}
</style>
